<?php

abstract class versoesDAOImpl {
    public static function Select($search) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "select id, name from versions";
            if (isset($search)) {
                if (is_int($search)) {
                    $query = $query . " where id=?";
                } else if(is_string($search)) {
                    $query = $query . " where name like ?";
                }
            }
            DB::$_stmt = DB::$mysqli->prepare($query);
            if (isset($search)) {
                if (is_int($search)) {
                    DB::$_stmt->bind_param('i', $search);
                } else if(is_string($search)) {
                    DB::$_stmt->bind_param('s', $search);
                }
            }
            if (!DB::$_stmt->execute()) {
                DB::$mysqli->error;
            }
            DB::$_stmt->bind_result($data_id,$data_name);
            while (DB::$_stmt->fetch()) {
                $data[] = array('id' => $data_id, 'name' => $data_name);
            }
            return $data;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Listar($limit=NULL, $page = NULL, $search = NULL) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "select id, name from versions";
            if (isset($search)) {
                if (is_numeric($search)) {
                    $query = $query . " where id=?";
                } else {
                    $query = $query . " where name like ?";
                }
            }
            if (isset($limit)) {
                $query = $query . " limit " . $limit;
            }
            DB::$_stmt = DB::$mysqli->prepare($query);
            if (isset($search)) {
                if (is_numeric($search)) {
                    DB::$_stmt->bind_param('i', $search);
                } else {
                    DB::$_stmt->bind_param('s', $search);
                }
            }
            if (!DB::$_stmt->execute()) {
                DB::$mysqli->error;
            }
            DB::$_stmt->bind_result($data_id, $data_name);
            while (DB::$_stmt->fetch()) {
                $data[] = array('id' => $data_id, 'name' => $data_name);
            }
            return $data;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Update($name,$id) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "update versions set name=?
                where id=?";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('si',$name,$id);
            DB::$_stmt->execute();
            return DB::$_stmt;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Insert($name) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "insert into versions
                (name) 
                values (?)";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('s',$name);
            if (!DB::$_stmt->execute()) {
                echo DB::$mysqli->error;
            }
            return DB::$mysqli->insert_id;
        } else {
            error_log("erro ao conectar!");
        }
    }

    public static function Delete($id) {
        if (DB::$mysqli = DB::Connection()) {
            $query = "delete from versions where id = ?";
            DB::$_stmt = DB::$mysqli->prepare($query);
            DB::$_stmt->bind_param('i', $id);
            DB::$_stmt->execute();
            return DB::$_stmt;
        } else {
            error_log("erro ao conectar!");
        }
    }

}

?>
